from rest_framework import serializers
from .models import *


class ManagerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Manager
        fields = ['id', 'manager_name', 'manager_tele', 'manager_account']


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'user_nick', 'user_tele', 'user_info', 'user_image', 'user_address']  # 全部字段进行序列化


class SuggestionMediaSerializer(serializers.ModelSerializer):
    class Meta:
        model = SuggestionMedia
        fields = ['id', 'sugg_media']


class SuggestionSerializer(serializers.ModelSerializer):
    suggestionmedia_set = SuggestionMediaSerializer(many=True, read_only=True)

    class Meta:
        model = Suggestion
        fields = '__all__'


class SuggestionSerializerUserAll(serializers.ModelSerializer):

    class Meta:
        model = Suggestion
        fields = '__all__'


class SuggestionSerializer_list(serializers.ModelSerializer):  # 给列表返回的字段，要简洁

    class Meta:
        model = Suggestion
        fields = ['id', 'sugg_name',
                  'sugg_site', 'sugg_sub_time', 'sugg_title']


class SuggestionFeedbackSerializer(serializers.ModelSerializer):
    class Meta:
        model = Suggestion
        fields = ['sugg_status', 'sugg_content', 'sugg_staff_name', 'sugg_way', 'sugg_handle_time',
                  'sugg_staff_tele', 'sugg_handle_id']

    def update(self, instance, validated_data):
        for field, value in validated_data.items():
            setattr(instance, field, value)
        instance.save()
        return instance


class SuggestionTreatSerializer(serializers.ModelSerializer):
    class Meta:
        model = Suggestion
        fields = ['sugg_treat', 'sugg_summary']

    def update(self, instance, validated_data):
        instance.sugg_treat = validated_data['sugg_treat']
        instance.sugg_summary = validated_data['sugg_summary']
        instance.save()
        return instance


class ComplaintMediaSerializer(serializers.ModelSerializer):

    class Meta:
        model = ComplaintMedia
        fields = ['id', 'comp_media']


class ComplaintSerializer(serializers.ModelSerializer):
    complaintmedia_set = ComplaintMediaSerializer(many=True)

    class Meta:
        model = Complaint
        fields = '__all__'


class ComplaintSerializerUserAll(serializers.ModelSerializer):

    class Meta:
        model = Complaint
        fields = '__all__'


class ComplaintSerializer_list(serializers.ModelSerializer):  # 给列表返回的字段，要简洁

    class Meta:
        model = Complaint
        fields = ['id', 'comp_name',
                  'comp_site', 'comp_sub_time', 'comp_title']


class ComplaintFeedbackSerializer(serializers.ModelSerializer):
    class Meta:
        model = Complaint
        fields = ['comp_status', 'comp_content', 'comp_staff_name', 'comp_way', 'comp_handle_time',
                  'comp_staff_tele', 'comp_handle_id']

    def update(self, instance, validated_data):
        for field, value in validated_data.items():
            setattr(instance, field, value)
        instance.save()
        return instance


class ComplaintTreatSerializer(serializers.ModelSerializer):
    class Meta:
        model = Complaint
        fields = ['comp_treat', 'comp_summary']

    def update(self, instance, validated_data):
        instance.comp_treat = validated_data['comp_treat']
        instance.comp_summary = validated_data['comp_summary']
        instance.save()
        return instance


class WarnSerializer(serializers.ModelSerializer):
    warn_text = serializers.CharField(allow_blank=True)

    # 前端没有textfield，只能用charfield
    # 如果是允许为空的字段，在序列化器这里要加allow_blank=True，不然还是会报错

    class Meta:
        model = Warn
        fields = '__all__'


class HotlineSerializer(serializers.ModelSerializer):
    hotline_who = serializers.CharField(max_length=20)
    hotline_tele = serializers.CharField(max_length=20)

    class Meta:
        model = Hotline
        fields = '__all__'


class CoverSerializer(serializers.ModelSerializer):
    class Meta:
        model = Cover
        fields = '__all__'


class BackSerializer(serializers.ModelSerializer):
    class Meta:
        model = Background
        fields = '__all__'

